package fr.xebia;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import xke.xwfc.biz.CallGirlBackend;
import xke.xwfc.biz.domain.CallGirl;
import xke.xwfc.biz.domain.HairColor;
import fr.xebia.beans.CallGirlSearchBean;

public class SearchForm extends Form {

	private static final long serialVersionUID = 1L;

	private static final List<Integer> BUDGET_COMBO_LIST = Arrays.asList(
			new Integer[] { 500, 1000, 1500, 2000, 2500, 3000,
					3500, 4000, 4500, 5000 });

	private static final List<String> HAIR_COLOR_COMBO_LIST = Arrays.asList(new String[] { "Black", "Blond", "Brown", "Red" });

	private static final Map<String, HairColor> hairColorMap = new HashMap<String, HairColor>();

	static {
		hairColorMap.put("Black", HairColor.BLACK);
		hairColorMap.put("Blond", HairColor.BLOND);
		hairColorMap.put("Brown", HairColor.BROWN);
		hairColorMap.put("Red", HairColor.RED);
	}

	private final CallGirlSearchBean callGirlSearchBean = new CallGirlSearchBean();

	public SearchForm(String id) {
		super(id);
		TextField prenomField = new TextField("prenom", new PropertyModel(callGirlSearchBean, "prenom"));
		add(prenomField);
		TextField ageMinField = new TextField("ageMin", new PropertyModel(callGirlSearchBean, "ageMin"));
		add(ageMinField);
		TextField ageMaxField = new TextField("ageMax", new PropertyModel(callGirlSearchBean, "ageMax"));
		add(ageMaxField);
		DropDownChoice hairColorChoice = new DropDownChoice("hairColor", new PropertyModel(callGirlSearchBean, "hairColor"),
				HAIR_COLOR_COMBO_LIST);
		add(hairColorChoice);
		DropDownChoice budgetMaxChoice = new DropDownChoice("budgetMax", new PropertyModel(callGirlSearchBean, "budgetMax"),
				BUDGET_COMBO_LIST);
		add(budgetMaxChoice);
	}

	/**
	 * @see org.apache.wicket.markup.html.form.Form#onSubmit()
	 */
	public final void onSubmit() {
		HairColor hairColor = null;
		if (hairColorMap.containsKey(callGirlSearchBean.getHairColor())) {
			hairColor = hairColorMap.get(callGirlSearchBean.getHairColor());
		}
		List<CallGirl> callGirls = CallGirlBackend.find(callGirlSearchBean.getPrenom(),
				callGirlSearchBean.getAgeMin(), callGirlSearchBean.getAgeMax(),
				hairColor,
				callGirlSearchBean.getBudgetMax());
		setResponsePage(new CataloguePage(callGirls));
	}

}
